/**  
 * All rights Reserved, Designed By www.flksh.com
 * @date:   2017-10-26 15:32:46   
 * @Copyright: 2013-2017 www.flksh.com Inc. All rights reserved. 
 * 注意：本内容仅限于上海福乐康社区养老服务有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
package com.github.prontera.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.quartz.CronExpression;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.prontera.ApiVersion;
import com.github.prontera.Shift;
import com.github.prontera.StatusCode;
import com.github.prontera.common.filter.CurrentUserId;
import com.github.prontera.domain.ScheduleJob;
import com.github.prontera.domain.ScheduleJobHistory;
import com.github.prontera.model.request.QuartzJobRequest;
import com.github.prontera.model.response.ObjectDataResponse;
import com.github.prontera.model.response.Response;
import com.github.prontera.service.ScheduleJobHistoryService;
import com.github.prontera.service.ScheduleJobService;
import com.github.prontera.util.Page;

import io.swagger.annotations.ApiOperation;

/**
 * 定时任务
 */
@RestController
@RequestMapping(value = ApiVersion.QUARTZ_V1_PREFIX + "/job", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE }, consumes = { MediaType.APPLICATION_JSON_UTF8_VALUE })
public class ScheduleJobController {

	@Autowired
	private ScheduleJobService scheduleJobService;
	@Autowired
	private ScheduleJobHistoryService scheduleJobHistoryService;

	@RequestMapping(value = "findHistory", method = RequestMethod.POST)
	@ResponseBody
	public Response findHistory(@CurrentUserId Long userId, @RequestBody ScheduleJobHistory scheduleJobHistory) {
		Page<ScheduleJobHistory> datas = scheduleJobHistoryService.page(scheduleJobHistory);
		return new ObjectDataResponse<Page<ScheduleJobHistory>>(datas);
	}

	@RequestMapping(value = "/find", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "查找定时任务", notes = "查找定时任务")
	public ObjectDataResponse<List<ScheduleJob>> find() throws SchedulerException {
		List<ScheduleJob> scheduleEntities = scheduleJobService.getAllScheduleJob();
		return new ObjectDataResponse<List<ScheduleJob>>(scheduleEntities);
	}

	/**
	 * 暂停任务
	 */
	@ResponseBody
	@RequestMapping(value = "/stopJob", method = RequestMethod.POST)
	@ApiOperation(value = "暂停定时任务", notes = "暂停定时任务")
	public ObjectDataResponse<Boolean> stop(@RequestBody QuartzJobRequest quartzJobRequest) {
		if (StringUtils.isEmpty(quartzJobRequest.getJobName())) {
			Shift.fatal(StatusCode.INVALID_MODEL_FIELDS, "JobName必传!");
		}
		if (StringUtils.isEmpty(quartzJobRequest.getJobGroup())) {
			Shift.fatal(StatusCode.INVALID_MODEL_FIELDS, "JobGroup必传!");
		}
		try {
			scheduleJobService.stopJob(quartzJobRequest.getJobName(), quartzJobRequest.getJobGroup());
		} catch (SchedulerException e) {
			e.printStackTrace();
			Shift.fatal(StatusCode.SERVER_UNKNOWN_ERROR, "添加定时任务错误,请重试!");
		}
		return new ObjectDataResponse<Boolean>(Boolean.TRUE);
	}

	/**
	 * 添加任务
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "添加定时任务", notes = "添加定时任务")
	public ObjectDataResponse<Boolean> add(@RequestBody ScheduleJob scheduleJob) throws ClassNotFoundException, SchedulerException {
		if (StringUtils.isEmpty(scheduleJob.getJobName())) {
			Shift.fatal(StatusCode.INVALID_MODEL_FIELDS, "JobName必传!");
		}
		if (StringUtils.isEmpty(scheduleJob.getJobGroup())) {
			Shift.fatal(StatusCode.INVALID_MODEL_FIELDS, "JobGroup必传!");
		}
		if (StringUtils.isEmpty(scheduleJob.getClassName())) {
			Shift.fatal(StatusCode.INVALID_MODEL_FIELDS, "ClassName必传!");
		}
		if (StringUtils.isEmpty(scheduleJob.getMethodName())) {
			Shift.fatal(StatusCode.INVALID_MODEL_FIELDS, "MethodName必传!");
		}
		if (StringUtils.isEmpty(scheduleJob.getCronExpression())) {
			Shift.fatal(StatusCode.INVALID_MODEL_FIELDS, "CronExpression必传!");
		}
		try {
			boolean f = CronExpression.isValidExpression(scheduleJob.getCronExpression());
			if (!f) {
				Shift.fatal(StatusCode.INVALID_MODEL_FIELDS, "cron表达式有误，不能被解析！");
			}
			scheduleJob.setStatus("1");
			scheduleJobService.add(scheduleJob);
		} catch (SchedulerException e) {
			e.printStackTrace();
			Shift.fatal(StatusCode.SERVER_UNKNOWN_ERROR, "添加定时任务错误,请重试!");
		}
		return new ObjectDataResponse<Boolean>(Boolean.TRUE);
	}

	/**
	 * 删除定时任务
	 * 
	 * @param quartzJobRequest
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "删除定时任务", notes = "删除定时任务")
	public ObjectDataResponse<Boolean> delete(@RequestBody QuartzJobRequest quartzJobRequest) {
		if (StringUtils.isEmpty(quartzJobRequest.getJobName())) {
			Shift.fatal(StatusCode.INVALID_MODEL_FIELDS, "JobName必传!");
		}
		if (StringUtils.isEmpty(quartzJobRequest.getJobGroup())) {
			Shift.fatal(StatusCode.INVALID_MODEL_FIELDS, "JobGroup必传!");
		}
		try {
			scheduleJobService.delJob(quartzJobRequest.getJobName(), quartzJobRequest.getJobGroup());
		} catch (SchedulerException e) {
			e.printStackTrace();
			Shift.fatal(StatusCode.SERVER_UNKNOWN_ERROR, "删除定时任务错误,请重试!");
		}
		return new ObjectDataResponse<Boolean>(Boolean.TRUE);
	}

	/**
	 * 立即运行一次
	 */
	@RequestMapping(value = "/startNow", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "立即运行一次定时任务", notes = "立即运行一次定时任务")
	public ObjectDataResponse<Boolean> startNowJob(@RequestBody QuartzJobRequest quartzJobRequest) {
		if (StringUtils.isEmpty(quartzJobRequest.getJobName())) {
			Shift.fatal(StatusCode.INVALID_MODEL_FIELDS, "JobName必传!");
		}
		if (StringUtils.isEmpty(quartzJobRequest.getJobGroup())) {
			Shift.fatal(StatusCode.INVALID_MODEL_FIELDS, "JobGroup必传!");
		}
		try {
			scheduleJobService.startNowJob(quartzJobRequest.getJobName(), quartzJobRequest.getJobGroup());
		} catch (SchedulerException e) {
			e.printStackTrace();
			Shift.fatal(StatusCode.SERVER_UNKNOWN_ERROR, "立即运行一次定时任务错误,请重试!");
		}
		return new ObjectDataResponse<Boolean>(Boolean.TRUE);
	}

	/**
	 * 恢复
	 */
	@RequestMapping(value = "/resume", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "恢复定时任务", notes = "恢复定时任务")
	public ObjectDataResponse<Boolean> resume(@RequestBody QuartzJobRequest quartzJobRequest) {
		if (StringUtils.isEmpty(quartzJobRequest.getJobName())) {
			Shift.fatal(StatusCode.INVALID_MODEL_FIELDS, "JobName必传!");
		}
		if (StringUtils.isEmpty(quartzJobRequest.getJobGroup())) {
			Shift.fatal(StatusCode.INVALID_MODEL_FIELDS, "JobGroup必传!");
		}
		try {
			scheduleJobService.restartJob(quartzJobRequest.getJobName(), quartzJobRequest.getJobGroup());
		} catch (SchedulerException e) {
			e.printStackTrace();
			Shift.fatal(StatusCode.SERVER_UNKNOWN_ERROR, "恢复定时任务错误,请重试!");
		}
		return new ObjectDataResponse<Boolean>(Boolean.TRUE);
	}

}
